package com.designpatterns.Balking;

import java.util.concurrent.TimeUnit;

/**
 * Created by 41579 on 2017/6/2.
 */
public class Data {
    private boolean flag;
    private String content;

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public synchronized void change(String content) {
        try {
            TimeUnit.SECONDS.sleep(5);
            this.flag = true;
            this.content = content;
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }

    public synchronized void save() {
        try {
            TimeUnit.SECONDS.sleep(4);
            if (flag) {
                doSave();
                this.flag = false;
            } else {
                return;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void doSave() {
        System.out.println("now the content is:"+content);
    }
}
